// pages/add_addr/add_addr.js
let app=getApp();
let openid = app.userList.openid;
let jumpurl=app.jumpurl.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("onload");
     var username;
     var tel;
     var addr_title;
     var address1="";
     var address2;
     var jingwei="";
    var that=this;
    if (wx.getStorageSync('username') == ''){
       username = '';
    }else{
       username = wx.getStorageSync('username');
    }
    if (wx.getStorageSync('tel') == '') {
       tel = '';
    } else {
       tel = wx.getStorageSync('tel');
    }
    if (wx.getStorageSync('address2') == '') {
       address2 = '';
    } else {
       address2 = wx.getStorageSync('address2');
    }

    wx.getStorage({
      key: 'map_add_key',
      success: function(res) {
        console.log("-------------map_add_key--------------");
        console.log(res.data);
           address1=res.data;
         that.setData({
           address1:address1
         })
      },
      fail:function(err){
        console.log("获取地址"+err);
        address1='';
      }

    })
    wx.getStorage({
      key: 'title_key',
      success: function (res) {
        console.log("------------add_title--------------");
        console.log(res.data);
        addr_title = res.data;
        that.setData({
          add_title: res.data
        })
      },
      fail: function (err) {
        console.log("获取地址" + err);
         addr_title = ""
      }
    })

    wx.getStorage({
      key: 'jingwei_key',
      success: function (res) {
       
       
        jingwei = res.data;
        that.setData({
          jingwei: res.data
        })
      },
      fail: function (err) {
        console.log("获取地址" + err);
        jingwei = '';
      }
    })
      if(!address1){
        address1="";
      }
      if(!addr_title){
        addr_title="";
      }
    that.setData({
      username: username,
      tel: tel,
      address2: address2,
      addr_title:addr_title,
      address1:address1
    })
  },
 
 onShow:function(){
   this.onLoad();
 },

  /*************跳转页面 选择地址 */
  choseaddr:function(e){
    var that=this;
   
    wx.setStorage({
      key: 'lujing',
      data: 'add_addr/add_addr',
    })
   
    wx.navigateTo({
      url: '/pages/address/address',
    })
    
  },



  formSubmit:function(e){
      console.log(e);
      console.log(openid);
      var username=e.detail.value.username;
      var tel=e.detail.value.tel;
      var address1=e.detail.value.address1;
      var address2=e.detail.value.address2;
      var jingwei=e.detail.value.jingwei;

    if (username.length == 0) {
      wx.showToast({
        title: '请输入联系人',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    if (tel.length == 0) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
   
    if (tel.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(tel)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1000
      })
      return false;
    }
    
    if(address1.length==0){
      wx.showToast({
        title: '请选择地址',
        icon:"none",
        duration:1000
      })
      return false;
    }
    
    if(address2.length==0){
      wx.showToast({
        title: '请输入详细地址',
        icon:"none",
        duration:1000
      })
      return false;
    }

    if(username && tel && address1 && address2){
      //请求接口
       wx.request({
         url: jumpurl+'often/add',
        data:{
          openid:app.userList.openid,
          name:username,
          tel:tel,
          map_add:address1,
          address:address2,
          jingwei:jingwei

        },
        success:function(res){
          wx.removeStorage({
            key: 'map_add_key',
            success: function(res) {
              console.log("清除map_add成功");
            },
          })
          wx.removeStorage({
            key: 'jingwei_key',
            success: function (res) {
              console.log("清除jingwei成功");
            },
          })
          wx.removeStorageSync('username');
          wx.removeStorageSync("tel");
          wx.removeStorageSync("address2");
      wx.showToast({
        title: '提交成功',
        icon:"success",
        duration:1000
      })
      setTimeout(function(){
        // wx.navigateTo({
        //   url: '/pages/addresslist/addresslist',
        // })
        // wx.redirectTo({
        //   url: '/pages/addresslist/addresslist',
        // })
        wx.navigateBack({
          delta:1
        })
      },1000)
        },
        fail:function(err){
          console.log(err);
        },
        complete:function(){
          console.log("complete");
        }
      })
    }

     

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('username');
    wx.removeStorageSync('tel');
    wx.removeStorageSync('address2');
    wx.clearStorage();
    wx.redirectTo({
      url: '/pages/addresslist/addresslist',
    })
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  username:function(e){
    wx.setStorageSync('username', e.detail.value);
  },
  tel: function (e) {
    wx.setStorageSync('tel', e.detail.value);
  },
  address2: function (e) {
    wx.setStorageSync('address2', e.detail.value);
  },
})